package com.uplooking.http;

import org.apache.http.HttpVersion;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

/**
 * 一个app只需要一个httpclient的时候使用此单例
 * @author zjingchuan
 *
 */
public class MyHttpClient {
	private static DefaultHttpClient defaultHttpClient;
	private MyHttpClient() {
		
	}
	public static DefaultHttpClient getHttpClient() {
		if (defaultHttpClient == null) {
			synchronized(DefaultHttpClient.class){
				if (defaultHttpClient == null) {
					HttpParams params = new BasicHttpParams();
					HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
					HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
					HttpProtocolParams.setUseExpectContinue(params, true);
					
					ConnManagerParams.setTimeout(params, 1000);
					HttpConnectionParams.setConnectionTimeout(params, 5000);
					HttpConnectionParams.setSoTimeout(params, 30*1000);
					
					SchemeRegistry schReg = new SchemeRegistry();
					schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
					schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

					ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);
					defaultHttpClient = new DefaultHttpClient(connMgr,params);
				}
			
			}
			
		}
		return  defaultHttpClient;
	}
}
